package com.mspbots.web.reports.service.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mspbots.web.BaseService;
import com.mspbots.web.reports.service.HealthReportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;


@Log4j2
@Service
@RequiredArgsConstructor
public class HealthReportServiceImpl extends BaseService implements HealthReportService {


    @Override
    public Mono<JsonNode> listTenant(Map<String, Object> queryParams) {
        return coreClient.get("health/list-tenant", queryParams);
    }

    @Override
    public Mono<JsonNode> queryTenantHealth(Map<String, Object> queryParams) {
        ObjectNode obj = coreClient.get("health/queryTenantHealth", queryParams, ObjectNode.class).block();
        try {
            obj = tenantHealth(obj);
        } catch (JsonProcessingException e) {
            log.info("queryTenantHealth JsonProcessingException:");
            e.printStackTrace();
        }
        return Mono.just(obj);
    }


    @Override
    public Flux<JsonNode> selectTenantAuthority(Map<String, Object> queryParams) {
        return coreClient.getFlux("health/queryTenantAuthority", queryParams);
    }


    @Override
    public ObjectNode tenantHealth(ObjectNode health) throws JsonProcessingException {

        ObjectNode singleNode = health;
        ObjectNode connectWiseSettingNode = this.objectMapper.readValue(singleNode.get("connectWiseSetting").toString(), ObjectNode.class).deepCopy();
        ObjectNode cwKeySettingObjNode = this.objectMapper.readValue(connectWiseSettingNode.get("cwKeySettingObj").toString(), ObjectNode.class).deepCopy();
        ObjectNode appIdNode = this.objectMapper.readValue(cwKeySettingObjNode.get("appId").toString(), ObjectNode.class).deepCopy();
        ObjectNode siteNode = this.objectMapper.readValue(cwKeySettingObjNode.get("site").toString(), ObjectNode.class).deepCopy();
        ObjectNode publicKeyNode = this.objectMapper.readValue(cwKeySettingObjNode.get("publicKey").toString(), ObjectNode.class).deepCopy();
        ObjectNode privateKeyNode = this.objectMapper.readValue(cwKeySettingObjNode.get("privateKey").toString(), ObjectNode.class).deepCopy();

        if (!appIdNode.get("isEmpty").asBoolean()
                && !siteNode.get("isEmpty").asBoolean()
                && !publicKeyNode.get("isEmpty").asBoolean()
                && !privateKeyNode.get("isEmpty").asBoolean()) {
            String result = "";
            try {
                ObjectNode paramObj = this.objectMapper.createObjectNode();
                paramObj.set("appId", appIdNode.get("value"));
                paramObj.set("site", siteNode.get("value"));
                paramObj.set("publicKey", publicKeyNode.get("value"));
                paramObj.set("privateKey", privateKeyNode.get("value"));
                paramObj.put("tenantId", singleNode.get("tenantId").asLong());
                log.info("wise exists/connect Exception:appIdNode " + appIdNode.get("value"));
                log.info("wise exists/connect Exception:siteNode " + siteNode.get("value"));
                log.info("wise exists/connect Exception:publicKeyNode " + publicKeyNode.get("value"));
                log.info("wise exists/connect Exception:privateKey " + privateKeyNode.get("value"));
                String str = wiseClient.post("exists/connect", paramObj, String.class).block();
                log.info("wise exists/connect Exception:str " + str);
                result = "block";
            } catch (Exception e) {
                result = "Exception";
                e.printStackTrace();
                log.info("wise exists/connect Exception:" + e.getMessage());
            } finally {
                if (result.equals("Exception")) {
                    cwKeySettingObjNode.put("cwKeySettingFlag", false);
                    cwKeySettingObjNode.put("message", "These settings have incorrect values, please check!");
                }
            }


        }
        cwKeySettingObjNode.set("appId", appIdNode);
        cwKeySettingObjNode.set("site", siteNode);
        cwKeySettingObjNode.set("publicKey", publicKeyNode);
        cwKeySettingObjNode.set("privateKey", privateKeyNode);
        connectWiseSettingNode.set("cwKeySettingObj", cwKeySettingObjNode);
        singleNode.set("connectWiseSetting", connectWiseSettingNode);
        return singleNode;
    }


}

